
import scrapy
from flask import current_app
from selenium import webdriver
from selenium.webdriver.chrome.options import Options

from app import evn_
from spiders.items import StockMarketItem
from utils.util import CalculationTool


class StockMarketSpider(scrapy.Spider):
    '''
    大盘行情（沪市，深市，创业板）

    http://quote.eastmoney.com/concept/sh603259.html#(个股行情)
    股票频道：http://stock.eastmoney.com/
    股票首页：http://finance.eastmoney.com/
     '''
    name = 'stock_market'
    start_urls = [
        'http://quote.eastmoney.com/zs000001.html',  # 沪市
        'http://quote.eastmoney.com/zs399001.html',  # 深圳
        'http://quote.eastmoney.com/zs399006.html'  # 创业板

    ]

    def __init__(self, *args, **kwargs):
        chrome_options = Options()

        chrome_options.add_argument('--disable-gpu')
        chrome_options.add_argument('lang=zh_CN.UTF-8')
        chrome_options.add_argument('headless')  # 无头浏览器
        chrome_options.add_argument('--no-sandbox')  # 必要！！
        chrome_options.add_argument('--disable-dev-shm-usage')  # 必要！！
        prefs = {
            "profile.managed_default_content_settings.images": 2,  # 禁止加载图片
            'permissions.default.stylesheet': 2,  # 禁止加载css
        }
        chrome_options.add_experimental_option("prefs", prefs)
        self.bro = webdriver.Chrome(executable_path=evn_.CHROMEDRIVER_PATH, chrome_options=chrome_options)
        # self.bro = Chrome()

    def parse(self, response):
        current_app.logger.info('{}'.format( response))

        # 大盘指数
        item = StockMarketItem()

        item['market_name'] = response.xpath('//*[@id="app"]/div/div/div[6]/div/span[1]/text()').extract_first()
        item['market_code'] = response.xpath('//*[@id="app"]/div/div/div[6]/div/span[2]/text()').extract_first()
        table_tb = response.xpath('//*[@id="app"]/div/div/div[9]/div[3]/div[2]/table[1]/tbody')
        item['open'] = table_tb.xpath('./tr[1]/td[2]/span/span/text()').extract_first()
        item['close'] = table_tb.xpath('./tr[1]/td[1]/span/span/text()').extract_first()
        item['high'] = table_tb.xpath('./tr[2]/td[1]/span/span/text()').extract_first()
        item['low'] = table_tb.xpath('./tr[2]/td[2]/span/span/text()').extract_first()
        # item['preclose'] = response.xpath('tr[1]/td[2]/span/text()').extract_first()
        item['pctChg'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[3]/td[1]/span/span/text()').extract_first())  # 涨跌幅
        item['volume'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[4]/td[1]/span/span/text()').extract_first())  # 亿手
        item['amount'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[4]/td[2]/span/span/text()').extract_first())  # 亿
        item['turn'] = CalculationTool.cutOutTail(table_tb.xpath('./tr[5]/td[1]/span/span/text()').extract_first())  # %
        item['peTTM'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[5]/td[2]/span/span/text()').extract_first())  # 流动市值  万亿

        item['inner'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[6]/td[1]/span/span/text()').extract_first())  # 万手
        item['outer'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[6]/td[2]/span/span/text()').extract_first())  # 万手
        item['amplitude'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[7]/td[1]/span/span/text()').extract_first())  # 振幅
        item['rising_nums'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[7]/td[2]/span/span/text()').extract_first())  # 家

        item['decliner_nums'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[8]/td[1]/span/span/text()').extract_first())  # 家
        item['flat_nums'] = CalculationTool.cutOutTail(
            table_tb.xpath('./tr[8]/td[2]/span/span/text()').extract_first())  # 家

        # 资金流入情况
        item['main_inflow'] = CalculationTool.convert2Y(response.xpath(
            '//*[@id="app"]/div/div/div[9]/div[3]/div[1]/div[2]/div/table[1]/tbody/tr[1]/td[2]/span/text()').extract_first())  # 主力流入 亿元
        item['main_outflow'] = CalculationTool.convert2Y(response.xpath(
            '//*[@id="app"]/div/div/div[9]/div[3]/div[1]/div[2]/div/table[1]/tbody/tr[2]/td[2]/span/text()').extract_first())  # 主流流出  亿元
        item['main_net_inflow'] = CalculationTool.convert2Y(
            response.xpath(
                '//*[@id="app"]/div/div/div[9]/div[3]/div[1]/div[2]/div/table[1]/tbody/tr[3]/td[2]/span/text()').extract_first())  # '主力资金净流入'   亿元
        # leading_stock = scrapy.Field()  # '领涨股票',
        date_span = response.xpath('//*[@id="app"]/div/div/div[6]/div/span[4]/text()').extract_first()
        if date_span:
            split = date_span.split(' ')
            item['create_time'] = split[0][1:]
            # item['week_day'] = self.common_week_day[split[1][-1]]
        yield item
